from rest_framework import serializers
from shopcar.models import Shopcar
from users.models import User
from goods.models import HuaweiGoods


class GoodsForShopcar(serializers.ModelSerializer):
    class Meta:
        model = HuaweiGoods
        fields = [
            "id",
            "goodName",
            "goodCoverImg",
            "goodPrice",
            "goodDetailNmae",
            "goodShipFree",
            "goodFavNums",
            "good_is_new",
            "good_is_hot",
        ]
        read_only_fields = [
            "id",
            "goodName",
            "goodCoverImg",
            "goodPrice",
            "goodDetailNmae",
            "goodShipFree",
            "goodFavNums",
            "good_is_new",
            "good_is_hot",
        ]


class UserForShopcar(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username"]


class ShopcarSerializer(serializers.ModelSerializer):
    shopcarowner = UserForShopcar()
    shopcargoods = GoodsForShopcar()

    class Meta:
        model = Shopcar
        fields = "__all__"
        read_only_fields = ["id", "shopcarowner", "shopcargoods", "add_time"]


class ShopcarSerializer_Post(serializers.ModelSerializer):
    class Meta:
        model = Shopcar
        fields = "__all__"
        read_only_fields = ["id", "shopcarowner", "add_time"]

    # def validate_shopcargoods(self,shopcargoods):
    #     if Shopcar.objects.filter(shopcargoods=shopcargoods).count():
    #         raise serializers.ValidationError("该商品已在购物车里了哦！")
    #     else:
    #         return shopcargoods
